package com.app.service.Impl;

import com.app.mapper.ArticleMapper;
import com.app.model.ArticleModel;
import com.app.model.CategoryModel;
import com.app.model.TownshipModel;
import com.app.service.ArticleService;
import com.app.service.CategoryService;
import com.app.service.TownService;
import com.app.vo.ArticleVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private TownService townService;
    @Autowired
    private CategoryService categoryService;

    @Override
    public void update(ArticleModel model) {
        articleMapper.updateById(model);
    }

    @Override
    public void insert(ArticleModel model) {
        articleMapper.insert(model);
    }

    @Override
    public void deleteById(Integer id) {
        articleMapper.deleteById(id);
    }

    @Override
    public ArticleVo selectList(Integer page, Integer limit, String keyword, Integer township, Integer type) {
        QueryWrapper<ArticleModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        queryWrapper.orderByDesc("sort").orderByDesc("id");
        if (type != null && type != 0) {
            queryWrapper.eq("type", type);
        }
        if (keyword != null && !keyword.equals("")) {
            queryWrapper.like("title", keyword);
        }
        Page<ArticleModel> page1 = new Page<>();
        page1.setCurrent(page);
        page1.setSize(limit);
        Page<ArticleModel> articleModelPage = articleMapper.selectPage(page1, queryWrapper);
        List<CategoryModel> categoryModelList = categoryService.getCategoryByPid(21);
        for (int i = 0; i < articleModelPage.getRecords().size(); i++) {
            for (int i1 = 0; i1 < categoryModelList.size(); i1++) {
                if (articleModelPage.getRecords().get(i).getType() != null && articleModelPage.getRecords().get(i).getType().equals(categoryModelList.get(i1).getId())) {
                    articleModelPage.getRecords().get(i).setTypeText(categoryModelList.get(i1).getName());
                }
            }
        }
        Integer count = articleMapper.selectCount(queryWrapper);
        ArticleVo vo = new ArticleVo();
        vo.setList(articleModelPage.getRecords());
        vo.setCount(count);
        return vo;
    }

    @Override
    public ArticleModel selectById(Integer id) {
        return articleMapper.selectById(id);
    }

    @Override
    public ArticleVo getArticleByType(Integer page, Integer limit, String title,Integer type,Integer cooperativeType) {
        QueryWrapper<ArticleModel> queryWrapper = new QueryWrapper<>();
        if (cooperativeType == 0){
            queryWrapper.eq("status", 1);
            queryWrapper.eq("type",type);
        }else {
            queryWrapper.eq("status", 1);
            queryWrapper.eq("type", type);
            queryWrapper.eq("cooperative_type", cooperativeType);
        }

        if (title != null && !title.equals("")) {
            queryWrapper.like("title", title);
        }
        Page<ArticleModel> page1 = new Page<>();
        page1.setCurrent(page);
        page1.setSize(limit);
        Page<ArticleModel> articleModelPage = articleMapper.selectPage(page1, queryWrapper);
        ArticleVo vo = new ArticleVo();
        vo.setList(articleModelPage.getRecords());
        vo.setCount((int) articleModelPage.getTotal());
        return vo;
    }
}
